<?php
/**
 * Created by PhpStorm.
 * User: allen
 * Date: 2019/3/8
 * Time: 10:38
 */
namespace app\index\controller;

use app\admin\model\MemberComment;
use app\admin\model\Proxy;
use app\common\controller\Encrypt;
use app\index\model\ArticleLog;
use app\index\model\ArticleProxy;
use app\index\model\CommentLog;
use app\index\model\Comments;
use app\index\model\HitsLog;
use app\index\model\Mch;
use app\index\model\Member;
use think\App;

class Article extends Base {

    public function __construct(App $app = null){
        parent::__construct($app);
    }

    /**
     * 获取列表
     * @return array|\think\response\Json
     * @throws \think\exception\DbException
     */
    public function index(){
        $model = new \app\index\model\Article();
        $limit= input('get.limit') ?? 10;
        $where['status']=2;
        $list = $model->getList($where,$limit,'id desc','id,title,image,description,extend,author,hits,create_time');
        if(!$list->total()){
//            return ['code'=>1,'msg'=>'没有数据'];
            return $this->__json(3,'没有数据');
        }
//        return ['code'=>0,'msg'=>'done','data'=>$list];
        return $this->__json(0,'done',$list);
    }

    /**
     * @param int $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function read($id=0){
        if(!$id){
           return ['code'=>1,'msg'=>'请输入id'];
        }
        $model = new \app\index\model\Article();
        $field = 'id,title,image,wechat_image,author,mcid as mc_id,finish_time,
        qrcode,description,extend,content,love,hits,hits1,create_time,status';
        $article = $model::field($field)->find($id);
        if(!$article){
            return $this->__json(1,'活动不存在');
        }
        if($article['status']==-1){
            return $this->__json(1,'活动已删除');
        }
        $article['is_finish'] = $article['finish_time']>time()?false:true;
		$count = (new \app\index\model\LikeLog())
            ->where('mid','eq',self::$uid)
            ->where('aid','eq',$id)->count();
		$article['is_like'] = $count ?? 0;
        $article['location'] = $model->getLocation($id);
        $model = new MemberComment();
        $article['is_can_reply'] =$model->getMemberReply($article['id'],self::$uid);
        $mch = new \app\admin\model\Mch();
//        $poxy = new Proxy();
//        $_poxy = $poxy->getPoxyMidByAid($article['id']);
//        $article['poxy_mid'] = $_poxy?$_poxy:0;
        $article['is_vip'] = $mch->is_vip($article['mc_id']);
        $article->hits1 = $article['hits1'] + 1;
        $hits = new HitsLog();
        if($hits->addNew(self::$uid,$article['id'])){
            $article->hits = $article['hits'] + 1;
        }
        $article->save();
//        return ['code'=>0,'msg'=>'done','data'=>$article];
        return $this->__json(0,'done',$article);
    }

    /**
     * @param int $id
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function like($id=0){
        if(!$id){
            return $this->__json(1,'请输入id');
        }
        $model = new \app\index\model\Article();
        $result = $model->like($id,self::$uid,$out);
        if(!$result){
//            return ['code'=>1,'msg'=>$out];
            $this->__json(1,$out);
        }
//        return ['code'=>0,'msg'=>$out];
        $this->__json(0,$out);
    }

    /**
     * 用户留言
     * @param int $id
     * @param string $content
     * @return array
     */
    public function comments($id=0,$content=''){
        $model = new \app\index\model\Article();
        $result = $model->comments($id,self::$uid,$content,$out);
        if(!$result){
            return ['code'=>1,'msg'=>$out];
        }
        return ['code'=>0,'msg'=>'留言成功'];
    }

    /**
     * @param $aid
     * @param int $page
     * @param int $limit
     * @return array
     * @throws \think\exception\DbException
     */
    public function comment_list($aid,$page=1,$limit=10){
        $model = new Comments();
        $where[] = ['think_comments.aid','eq',$aid];
        $where[] = ['think_comments.status','eq',2];
        $field = 'think_comments.id,think_comments.love,think_comments.content,think_comments.create_time,
        think_member.nickname,think_member.avatar';
        $order = 'think_comments.is_top desc,think_comments.create_time desc';
        $list = $model->getList($where,$field,$order,$limit);
        $log = new CommentLog();
        foreach ($list as $k =>$v){
            $list[$k]['is_like']= $log
                ->where('cid','eq',$v['id'])
                ->where('mid','eq',self::$uid)
                ->count();
        }
    }

    /**
     * @param $aid
     * @param int $limit
     * @throws \think\exception\DbException
     */
    public function clist($aid,$limit=10){
        $model = new Comments();
        $where[] = ['think_comments.aid','eq',$aid];
        $where[] = ['think_comments.status','eq',1];
        $list = $model->getList1($where,'create_time desc',$limit);
        $log = new CommentLog();
        foreach ($list as $k =>$v){
            $list[$k]['is_like'] =  $log
                ->where('cid','eq',$v['id'])
                ->where('mid','eq',self::$uid)
                ->count() ?? 0;
        }
        $data = $list->total();
        if(!$data){
            $this->__json(2,'done');
        }
        $this->__json(0,'done',$list);
    }


    /**
     * @param int $id
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function love($id=0){
        $model = new Comments();
        $result = $model->love($id,self::$uid,$out);
        if(!$result){
            $this->__json(1,$out);
        }
        $this->__json(0,$out);
    }

    /**
     * @param int $id
     * @param string $content
     */
    public function reply($id=0,$content=''){
        $model = new Comments();
        $result = $model->reply($id,self::$uid,$content,$out);
        if(!$result){
            $this->__json(1,$out);
        }
        $this->__json(0,'回复成功');
    }

    /**
     *商户申请
     * @return array
     */
    public function mch(){
        $data = request()->post();
        $data['uid']=self::$uid;
        $data['username']=$data['name'];
        $validate = new \app\index\validate\Mch();
        if(!$validate->check($data)){
            return ['code'=>1,'msg'=>$validate->getError()];
        }
        $model = new Mch();
        $count = $model::where('uid','eq',$data['uid'])->count('id');
        if($count){
            return ['code'=>1,'msg'=>'您已经申请了，请耐心等待审核'];
        }
        $data['create_time']=time();

        $data['password']= self::_password('123456');
        if(!$model->insert($data)){
            $this->__json(1,'申请失败');
        }
        $this->__json(0,'申请成功');
    }

    /**
     * @return array
     * @throws \think\Exception\DbException
     */
    public function mch_info(){
        $model = new Mch();
        $info = $model
            ->field('id,name,company,tel,status,create_time')
            ->where('uid','eq',self::$uid)
            ->find();
		if(!$info){
		    $this->__json(2,'done');
		}
		$this->__json(0,'done',$info);
    }

    /**
     * 获取可回复数据
     * @param $id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_reply_list($id=0){
        if(!$id){
            return $this->__json(1,'缺少参数');
        }
        $m = new \app\index\model\Article();
        $list = $m->get_reply($id);
        if(!$list){
            $this->__json(2,'没有数据');
        }
        $this->__json(0,'done',$list);
    }

    /**
     * 分享记录
     * @param int $id
     * @param int $mid
     * @param int $mc_id
     */
    public function share($id=0,$mid=0,$mc_id=0){
        if(!$id || !$mc_id || !$mid){
            return $this->__json(1,'缺少参数');
        }
        $mid = Encrypt::decode($mid);
        $log = new ArticleLog();
        if($log->check($id,self::$uid,$mid,$mc_id)){
           return $this->__json(2,'已经记录了');
        }
        $result = $log->addLog($mid,self::$uid,$id,$mc_id,$out);
        if(!$result){
            return $this->__json(1,$out);
        }
        return $this->__json(0,'记录成功');
    }

    /**
     * 申请代理
     * @param int $mc_id
     * @param string $tel
     * @param string $name
     */
    public function proxy($mc_id=0,$tel='',$name=''){
        if(!$tel || !$mc_id){
            return $this->__json(1,'缺少参数');
        }
        $m = new ArticleProxy();
        $result = $m->addNew(self::$uid,$mc_id,$name,$tel,$out);
        if(!$result){
            return $this->__json(1,$out);
        }
        return $this->__json(0,'申请成功，等待商家核准',$result);
    }

    /**
     * @param int $mc_id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function proxy_info($mc_id=0){
        if(!$mc_id){
            return $this->__json(1,'缺少参数');
        }
        $info = (new ArticleProxy())
            ->field('id,name,tel,info,status')
            ->where('mid','eq',self::$uid)
            ->where('mc_id','eq',$mc_id)
            ->find();
        if(!$info){
            return $this->__json(2,'申请不存在');
        }
        return $this->__json(0,'done',$info);
    }
}